﻿using System;
using System.Collections.Generic;

using System.Text;
using HZ.DrugTrade;
using HZ.DrugTrade.IBLL;
using HZ.Common;
using HZ.DrugTrade.DAL;
using System.Data;


namespace HZ.DrugTrade.BLL
{
    public class BLL_Statistics : MarshalByRefObject, IBLL_Statistics
    {

        /// <summary>
        /// 获取每个企业总汇总信息
        /// </summary>
        /// <param name="where"></param>
        /// <param name="currenpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public IList<Model.View.Statistics_Company> GetStatistics_CompanyInfo(Model.Query.Statistics_Company where, int currenpage, int pagesize, out int totalrecord)
        {
            ThrowHelper.Throw(where.CompanyID_PS != null, "where.CompanyID_PS != null,");
            return HZ.DrugTrade.DAL2.DAL_Helper.GetPagedView<Model.View.Statistics_Company, Model.Query.Statistics_Company>(where, currenpage, pagesize, out totalrecord);
        }

        /// <summary>
        /// 获取每个企业各家医疗机构总汇总信息
        /// </summary>
        /// <param name="where"></param>
        /// <param name="currenpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public IList<Model.View.Statistics_Company_Hospit> GetStatistics_Company_Hospital(Model.Query.Statistics_Company_Hospit where, int currenpage, int pagesize, out int totalrecord)
        {
          //  ThrowHelper.Throw(where.CompanyID_PS != null, "where.CompanyID_PS != null,");
          //  return HZ.DrugTrade.DAL2.DAL_Helper.GetPagedView<Model.View.Statistics_Company_Hospit, Model.Query.Statistics_Company_Hospit>(where, currenpage, pagesize, out totalrecord);
            string condition = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition(where, true);

            HZ.Data.PageDescribe pdes = new HZ.Data.PageDescribe(string.Format("SELECT * FROM View_{1} WHERE  1=1  {0} ", condition, typeof(HZ.DrugTrade.Model.View.Statistics_Company_Hospit).Name), pagesize);
            pdes.CurrentPage = currenpage;
            totalrecord = pdes.TotalCount;

            System.Data.DataTable dt = pdes.GetSpecailPage();
            return HZ.Data.DAL_Helper.CommonFillList<Model.View.Statistics_Company_Hospit>(dt); ;
        }

        /// <summary>
        /// 获取每个医疗机构所有产品汇总信息
        /// </summary>
        /// <param name="where"></param>
        /// <param name="currenpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public IList<Model.View.Statistics_Hospital> GetStatistics_Hospital(Model.Query.Statistics_Hospital where, int currentpage, int pagesize, out int totalrecord)
        {
            //ThrowHelper.Throw(where.HospitalID != null, "where.HospitalID != null,");
            //增加按时间段查询
            string condition = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition(where,false);
            string sql = string.Format(@"SELECT     HospitalID, ProcureCatalogID,projectid, MAX(GoodsID)  GoodsID,
                      MAX(Outlookc)  Outlookc, MAX(CompanyName_SC)  CompanyName_SC, MAX(Unit)  Unit, MAX(SortName)  SortName, 
                      MAX(companyName_TB)  companyName_TB, MAX(GoodsName)  GoodsName, MAX(Factor)  Factor, MAX(Pack)  Pack, MAX(Material) 
                       Material, MAX(ProcurePrice)  ProcurePrice, MAX(PinyinCode)  PinyinCode, MAX(QualityLevel)  QualityLevel, SUM(PurchaseCount) 
                       PurchaseCountSum, SUM(DistributeCount)  DistributeCountSum, SUM(WarehouseCount)  WarehouseCountSum, SUM(PurchaseCount * Price) 
                       PurchaseAmountSum, SUM(WarehouseCount * Price)  WarehouseAmountSum, COUNT(*)  num
FROM         View_PurchaseOrderDetail 
where 1=1 {0} 
GROUP BY HospitalID, ProcureCatalogID,projectid ORDER BY {1}", condition, where.SortFields);

            HZ.Data.PageDescribe pdes = new HZ.Data.PageDescribe(sql, pagesize);
            pdes.CurrentPage = currentpage;
            totalrecord = pdes.TotalCount;

            DataTable dt = pdes.GetSpecailPage();
            return HZ.Data.DAL_Helper.CommonFillList<HZ.DrugTrade.Model.View.Statistics_Hospital>(dt);
            
            //return HZ.DrugTrade.DAL2.DAL_Helper.GetPagedView<Model.View.Statistics_Hospital, Model.Query.Statistics_Hospital>(where, currentpage, pagesize, out totalrecord);
        }
    }
}
